import requests
from bs4 import BeautifulSoup

url='https://www.lgfdcw.com/cs/'

h={'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'}


r=requests.get(url,headers=h)
html=r.content.decode('gbk')
data=[]
soup=BeautifulSoup(html,'lxml')
# tr=soup.find_all('tr',{'bgcolor':'#FFFFFF'})
td=soup.find_all('tr',{'bgcolor':'#FFFFFF'})
for i in td:
    h={}
    h['详细地址']=i.find('a',{'target':'_blank'}).string
    h['区域']=i.select('td')[1].string
    h['房型'] = i.select('td')[2].string
    h['户型'] = i.select('td')[3].string
    h['面积'] = i.select('td')[4].string
    h['出售价格'] = i.select('td')[5].string
    h['登记时间'] = i.select('td')[6].string
    data.append(h)
with open('data1.txt','w',encoding='utf-8') as f:
    f.write(data.__str__())